﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace ManagementSystem
{
    /// <summary>
    /// 修改学生信息窗体
    /// </summary>
    public partial class EditStudent : Form
    {
        //调用服务
        private readonly SystemServices services = new SystemServices();

        //记录当前ID
        private string ID;

        public EditStudent()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 构造函数，从上级窗体导入ID
        /// </summary>
        /// <param name="ID"></param>
        public EditStudent(string ID)
        {
            InitializeComponent();

            // 使用传入的 ID 查询对应的信息，并填充到文本框中
            var item = services.GetBysID(ID);
            if (item != null)
            {
                textBoxID.Text = item.ID;
                textBoxName.Text = item.Name;
                textBoxClass.Text = item.Class;
                textBoxPwd.Text = item.Password;
                textBoxHometown.Text = item.Hometown;
                this.ID = ID;
            }
        }

        /// <summary>
        /// 取消操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 保存修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            // 调用 services 对象的 Edits 方法，传递新创建的 models 模型实例以更新数据库中的记录
            EditModels models = new EditModels()
            {
                ID = this.ID,
                Name = textBoxName.Text,
                Class = textBoxClass.Text,
                Password = textBoxPwd.Text,
                Hometown = textBoxHometown.Text,
            };

            // 调用 services 对象的 Edits 方法，传递新创建的 models 模型实例以更新数据库中的记录
            services.Edits(models);

            // 设置当前窗体的对话结果为 OK，并关闭窗体
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        //更新
        public bool SaveChanges()
        {
            return true;
        }

        private void EditStudent_Load(object sender, EventArgs e)
        {

        }
    }
}
